/**
 * Copyright 2024 Inc.
 **/
package com.myz.java.study.juc;

import java.util.concurrent.BlockingQueue;

/**
 * @author maoyz0621 on 2024/4/25
 * @version v1.0
 */
public class DelayConsumer implements Runnable {

    private final BlockingQueue<DelayContext> queue;

    public DelayConsumer(BlockingQueue<DelayContext> blockingDeque) {
        this.queue = blockingDeque;
    }

    @Override
    public void run() {
        while (true) {
            DelayContext peek = queue.peek();
            if (peek != null && peek.getAvailableTime() <= System.currentTimeMillis()) {
                DelayContext poll = queue.poll();
                if (peek.equals(poll)) {
                    System.out.println("11111111111111111");
                } else {
                    System.out.println("22222");
                }
            } else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}